package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.callshow.CallShowEntry;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.WallpaperAdapter;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.ActivityWallpaperBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

public class WallpaperActivity extends BaseAc<ActivityWallpaperBinding> {

    public static List<StkResBean> listWallpaper;
    public static int selPosition;
    private WallpaperAdapter wallpaperAdapter;
    private boolean isShow = false;
    private int num;
    private boolean isCollect;
    private List<StkResBean> listSp;

    @Override
    protected int onCreate() {
        return R.layout.activity_wallpaper;
    }

    @Override
    protected void initView() {
        mDataBinding.ivWallpaperBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivWallpaperImg.setOnClickListener(this);
        mDataBinding.ivWallpaperCollect.setOnClickListener(this);
        mDataBinding.ivWallpaperShare.setOnClickListener(this);
        mDataBinding.ivWallpaperPreview.setOnClickListener(this);
        mDataBinding.ivWallpaperApply.setOnClickListener(this);
        mDataBinding.rvWallpaperList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        wallpaperAdapter = new WallpaperAdapter();
        mDataBinding.rvWallpaperList.setAdapter(wallpaperAdapter);
        wallpaperAdapter.setOnItemClickListener(this);
        List<StkResBean> list = new ArrayList<>();
        for (StkResBean bean : listWallpaper) {
            bean.setSelected(false);
            list.add(bean);
        }
        list.get(selPosition).setSelected(true);
        wallpaperAdapter.setList(list);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivWallpaperImg:
                if (isShow) {
                    isShow = false;
                    mDataBinding.llWallPaperShow.setVisibility(View.GONE);
                } else {
                    isShow = true;
                    mDataBinding.llWallPaperShow.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.ivWallpaperShare://分享
                IntentUtil.shareText(mContext, listWallpaper.get(selPosition).getUrl());
                break;
            case R.id.ivWallpaperCollect://收藏
                if (isCollect){
                    listSp.remove(listWallpaper.get(selPosition));
                    AppSpUtils.saveCollectList(listSp);
                    isCollect=false;
                    num--;
                    mDataBinding.tvWallpaperCollectNum.setText(getString(R.string.collect_num, num));
                    mDataBinding.ivWallpaperCollect.setImageResource(R.drawable.ashoucangwei);
                }else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(listWallpaper.get(selPosition));
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        AppSpUtils.saveCollectList(listSave);
                    }
                    isCollect=true;
                    num++;
                    mDataBinding.tvWallpaperCollectNum.setText(getString(R.string.collect_num, num));
                    mDataBinding.ivWallpaperCollect.setImageResource(R.drawable.ayishoucang);
                }
                break;
            case R.id.ivWallpaperPreview://预览
                WallpaperPreviewActivity.url=listWallpaper.get(selPosition).getUrl();
                startActivity(WallpaperPreviewActivity.class);
                break;
            case R.id.ivWallpaperApply://应用壁纸
                Downloader.newTask(mContext)
                        .url(listWallpaper.get(selPosition).getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".jpg")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                CallShowEntry.setWallpaper(uri, new CallShowEntry.SetWallpaperCallback() {
                                    @Override
                                    public void onStart() {
                                        showDialog(getString(R.string.setting_wallpaper));
                                    }

                                    @Override
                                    public void onSetResult(boolean success) {
                                        dismissDialog();
                                        if (success) {
                                            ToastUtils.showShort(getString(R.string.setting_suc));
                                        } else {
                                            ToastUtils.showShort(R.string.setting_failure);
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                ToastUtils.showShort(R.string.setting_failure);
                            }
                        });
                saveRecord(wallpaperAdapter.getItem(selPosition));
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        wallpaperAdapter.getItem(selPosition).setSelected(false);
        wallpaperAdapter.notifyItemChanged(selPosition);
        selPosition=position;
        wallpaperAdapter.getItem(position).setSelected(true);
        wallpaperAdapter.notifyItemChanged(position);
        getData();
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        Glide.with(mContext).load(listWallpaper.get(selPosition).getUrl()).into(mDataBinding.ivWallpaperImg);
        Random random = new Random();
        num = 1000 + random.nextInt(1000);
        mDataBinding.tvWallpaperCollectNum.setText(getString(R.string.collect_num, num));
        listSp = AppSpUtils.getCollectList();
        isCollect = false;
        mDataBinding.ivWallpaperCollect.setImageResource(R.drawable.ashoucangwei);
        if (listSp != null && listSp.size() != 0) {
            for (StkResBean bean : listSp) {
                if (bean.equals(listWallpaper.get(selPosition))) {
                    isCollect = true;
                    mDataBinding.ivWallpaperCollect.setImageResource(R.drawable.ayishoucang);
                    break;
                }
            }
        }
    }

    //保存记录
    private void saveRecord(StkResBean bean) {
        List<StkResBean> listSave = new ArrayList<>();
        bean.setSelected(false);
        listSave.add(bean);
        List<StkResBean> listSp = AppSpUtils.getUseRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveUseRecordList(listSp);
        } else {
            AppSpUtils.saveUseRecordList(listSave);
        }
    }

}